class ContentTypeFilters {
	def filters = {
		contentTypeCheck(controller:'*', action:'*') {
			before = {
				def clientRequest = request.getHeader("accept")
				def serverResponse = request.format
				println "Client: ${clientRequest}\nServer: ${serverResponse}\n"    
				
					if (!isSupported(request.getHeader("accept"))){
						response.status = 406
						render ""
						return false
					} 
					if (!request.format in (['json','all']) ){
						response.status = 400
						render ""
						return false
					}
					return true
				}
			}
		} 
	
	def isSupported(contentType){
	
		println "contentType:$contentType"
		def result = false
		if (contentType){
			def mimeTypes = ['application/json','text/json','*/*']
			     mimeTypes.each { g ->
								 println "g:$g"
								if ((contentType == null) || contentType?.contains(g) ){
										result = true
								   }
					}
		 }else{
			 result = true
		}
		return result
	}
	
}	